package com.clz.video.mobilevideo.launcher;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.clz.appdesign.appdependence.base.BaseActivity;
import com.clz.appdesign.appdependence.launcher.BaseTimeTask;
import com.clz.appdesign.appdependence.launcher.OnTimeListenter;
import com.clz.video.mobilevideo.MainActivity;
import com.clz.video.mobilevideo.R;

import java.text.MessageFormat;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/11.
 * 启动页
 */

public class SplashActivity extends BaseActivity implements OnTimeListenter {
    @BindView(R.id.iv_one)
    ImageView mIvOne;
    @BindView(R.id.iv_two)
    ImageView mIvTwo;
    @BindView(R.id.tv_skip)
    AppCompatTextView mTvSkip;
    private Handler mHandler = new Handler();
    private TimerTask mTimerTask = null;
    private Timer mTimer;
    private int mCount = 5;

    @Override
    public void initToolbar() {

    }


    @Override
    public void initView() {
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public void initData() {
        if (mTimer == null) {
            mTimer = new Timer();
        }
    }

    private static final String imgUrl = "http://e.hiphotos.baidu.com/image/pic/item/500fd9f9d72a6059099ccd5a2334349b023bbae5.jpg";

    @Override
    public void doBusiness(Context mContext) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvSkip.setVisibility(View.VISIBLE);
                        Glide.with(SplashActivity.this).load(imgUrl).into(mIvTwo);
                    }
                });
                if (mTimerTask == null) {
                    mTimerTask = new BaseTimeTask(SplashActivity.this);
                    mTimer.schedule(mTimerTask, 0, 1000);
                }

            }
        }, 2000);
    }

    @Override
    public int layoutResID() {
        return R.layout.activity_splash;
    }


    @OnClick(R.id.tv_skip)
    public void onViewClicked() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            if (mHandler != null) {
                mHandler = null;
            }
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

    }

    @Override
    public void excuteTask() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTvSkip.setText(MessageFormat.format("跳过\n{0}", mCount--));
                if (mCount < 0) {
                    if (mTimer != null) {
                        mTimer.cancel();
                        mTimer = null;
                        if (mHandler != null) {
                            mHandler = null;
                        }
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    }
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
